#include <stdio.h>                      /* printf */
#include <stdint.h>                     /* uint8_t */
#include <stdlib.h>                     /* malloc */

#include "maz/assert.h"
#include "maz/print.h"
#include "relength.h"

static int dlvl = DINF;

#define ILEN                            1280
#define OLEN                            2048

#define LOOP                            16

int main(int argc, char **argv)
{
    int i = 0;
    int ret = 0;
    RELENGTH_HANDLE handle = { 0 };
    RELENGTH_FRAME iframe = { 0 };
    RELENGTH_FRAME oframe = { 0 };

    uint8_t *ibuf = malloc(ILEN);
    uint8_t *obuf = malloc(OLEN);

    /* 16k采样率/16bit位深/1M缓冲池 */
    ret = relength_init(&handle, 16000, 2, 1024 * 1024);
    MAZASSERT_RETVAL(ret, -1, "err: relength_init, ret = %d", ret);
    dlog(DBUG, "success: relength init");

    iframe.pts = 0;
    iframe.len = ILEN;
    iframe.buf = ibuf;

    oframe.len = OLEN;
    oframe.pts = 0;
    oframe.buf = obuf;

    for(i = 0; i < LOOP; i++)
    {
        iframe.pts += 40000;
        ret = relength_write(&handle, &iframe);
        MAZASSERT_RETVAL(ret, -1, "err: relength_write, ret = %d", ret);
        dlog(DBUG, "success: relength write");

        while(!relength_read(&handle, &oframe))
        {
            dlog(DBUG, "success: relength read");
            dlog(DINF, "frame pts = %lu", oframe.pts / 1000);
        }
    }

    return 0;
}

